package org.ams.common.task;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.ams.common.entity.SystemMoniterCache;
import org.ams.common.util.Common;
import org.ams.common.util.SigarUtil;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SystemMonitorTask {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemMonitorTask.class);

	@Scheduled(cron="0/5 * * * * ? ")
	public void loadSystemMonitorInfo(){
		/** 获取服务器ip地址 start **/
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String ip = addr.getHostAddress();
			SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.HOST_IP, ip);
		} catch (UnknownHostException uhe) {
			logger.error(uhe.getMessage());
		}  
		/** 获取服务器ip地址 end **/
		
		/** 获取服务器操作系统名称 start **/
		OperatingSystem OS = OperatingSystem.getInstance();  
		String osName = OS.getVendorName();
		SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.OS_NAME, osName);
		/** 获取服务器操作系统名称 end **/
		
		/** 获取JVM信息 start **/
		Runtime run = Runtime.getRuntime();
		Long jvmTotal = run.totalMemory();
		Long jvmFree = run.freeMemory();
		Long jvmUsed = jvmTotal - jvmFree;
		Double jvmUsedRate = Common.rate(jvmUsed, jvmTotal);
		Double jvmFreeRate = Common.rate(jvmFree, jvmTotal);
		SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.JVM_TOTAL, jvmTotal);
		SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.JVM_USED, jvmUsed);
		SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.JVM_FREE, jvmFree);
		SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.JVM_USED_RATE, jvmUsedRate);
		SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.JVM_FREE_RATE, jvmFreeRate);
				
        /** 获取JVM信息 end **/
        
		Sigar sigar = SigarUtil.getInstance();
		/** 获取内存相关信息 **/
		Mem mem;
		try {
			mem = sigar.getMem();
			// 内存总量 (KB)
			Long memTotal = mem.getTotal() / 1024L;
			// 当前内存使用量  (KB)
			Long memUsed = mem.getUsed() / 1024L;
			// 当前内存剩余量   (KB)
			Long memFree = mem.getFree() / 1024L;
			// 内存使用率
			Double memUsedRate = Common.rate(memUsed, memTotal);
			// 内存空闲率
			Double memFreeRate = Common.rate(memFree, memTotal);
			SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.MEMORY_TOTAL, memTotal);
			SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.MEMORY_USED, memUsed);
			SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.MEMORY_FREE, memFree);
			SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.MEMORY_USED_RATE, memUsedRate);
			SystemMoniterCache.systemMonitorCache.put(SystemMoniterCache.MEMORY_FREE_RATE, memFreeRate);
			
		} catch (Exception e) {
			logger.error(e.getMessage());
		}  
	        
	}
}
